package cn.cmft.jc.transform.parse.strategy;

import cn.cmft.jc.transform.parse.ChangeDetail;
import net.sf.jsqlparser.statement.alter.AlterExpression;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;


/**
 * desc 处理ADD : 增加列/约束/索引
 */
public class AddColumnParser implements AlterExpressionParser {
    @Override
    public List<ChangeDetail> parse(AlterExpression alterExpression) {
        ArrayList<ChangeDetail> columns = new ArrayList<>();
        Optional<List<AlterExpression.ColumnDataType>> colDataTypeListOp = Optional.ofNullable(alterExpression.getColDataTypeList());
        if (colDataTypeListOp.isPresent()){
            List<AlterExpression.ColumnDataType> colDataTypeList = colDataTypeListOp.get();
            for (AlterExpression.ColumnDataType colDataType : colDataTypeList) {
                String colName = colDataType.getColumnName();
                String colType = colDataType.getColDataType().toString();
                columns.add(new ChangeDetail(colName.replace("`",""),colType));
            }
        }
        return columns;
    }
}
